package com.whoru.app;

import com.whoru.beans.*;
import com.whoru.logica.WebServices;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;


public class Login extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
       
        TextView msg = (TextView) findViewById(R.id.error);
        Bundle bundle;
       
        if (getIntent().getExtras() != null)
        {
            bundle = getIntent().getExtras();
            String error = bundle.getString("Error");
            msg.setText(error);
        }
    }
   
    public void OnLogin (View formu) {
        try{   
            EditText nombre = (EditText) findViewById(R.id.editText1);
            EditText pass = (EditText) findViewById(R.id.editText2);
        //    ProgressBar progress = (ProgressBar) findViewById(R.id.progressBar1);
        //    progress.setVisibility(1);
            Intent inten= new Intent();
           
            User user;
            WebServices ws = new WebServices();
           
            user = ws.getUser(nombre.getText().toString(), pass.getText().toString());
        
            if (user.getName() == "")
            {
                inten.setClass(this, Login.class);
                inten.putExtra("Error", getString(R.string.invalidLogin));
                startActivity(inten);
                finish();
            }
            else
            {  
                Globals appState = ((Globals)getApplicationContext());
                appState.setUser(user);
               
                //appState.setAmigos(ws.getAmigos(user.getUid()));
                //appState.setDesconocidos(ws.getDescos(user.getUid()));
                //appState.setEstablecimientos(ws.getEstablecimientos(user.getUid()));
               
                //appState.setNotificaciones(ws.getNotificaciones(user.getUid()));
                appState.setFiltU(new FiltroUser());
                appState.setFiltE(new FiltroEstablecimiento());
               
                inten.setClass(this, Whoru.class);
                startActivity(inten);
                finish();
            }   
           
        }catch (Exception e){System.out.println("Errorea");e.printStackTrace();}
    }
    
    public void OnReset (View formu) {
    	
  	  Intent refresh = new Intent(this, Login.class);
	      startActivity(refresh);	
	      finish();
    }
}

